//
//  SubmitDetialTableViewController.m
//  Consume100
//
//  Created by eliot on 16/3/29.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "SubmitDetialTableViewController.h"
#import "SubmitDetailTableViewCell.h"
#import "ExchangeSucceedViewController.h"
#import "AreaViewController.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"

#import "SecurityUtil.h"
#import "GTMBase64.h"

@interface SubmitDetialTableViewController ()

@property (nonatomic, strong) NSString *area;

/** 收件人*/
@property (nonatomic, strong) UITextField *personTF;

/** 联系电话*/
@property (nonatomic, strong) UITextField *phoneTF;

/** 地址*/
@property (nonatomic, strong) UITextField *addressTF;

/** 详细地址*/
@property (nonatomic, strong) UITextField *detailAddressTF;

@end

@implementation SubmitDetialTableViewController


-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

    [self.view endEditing:YES];
}

- (UITextField *)personTF {
    if(!_personTF){
        _personTF = [[UITextField alloc] init];
    }
    return _personTF;
}

- (UITextField *)phoneTF {
    if(!_phoneTF){
        _phoneTF = [[UITextField alloc] init];
    }
    return _phoneTF;
}
- (UITextField *)addressTF {
    if(!_addressTF){
        _addressTF = [[UITextField alloc] init];
    }
    return _addressTF;
}
- (UITextField *)detailAddressTF {
    if(!_detailAddressTF){
        _detailAddressTF = [[UITextField alloc] init];
    }
    return _detailAddressTF;
}



+(SubmitDetialTableViewController*)CreatSubmitDetialTableViewController{
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    SubmitDetialTableViewController *SubmitDetialTableViewController=[story instantiateViewControllerWithIdentifier:@"SubmitDetialTableViewController"];
    
    return SubmitDetialTableViewController;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SubmitDetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"SubmitDetailTableViewCell"];
    
    self.title = @"提交订单";
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
    /** 设置导航啦样式*/
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

//创建模态导航控制器
-(void)creatNav:(UIViewController *)view{
    
    UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:view];
    nav.navigationBar.barTintColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
    view.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 5;
    }else if (section == 1){
        return 3;
    }else {
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    tableView.showsVerticalScrollIndicator = NO;
    if (indexPath.section == 0) {
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if ( !cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        }
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        switch (indexPath.row) {
            case 0:
            {
                cell.textLabel.text = @"收货信息";
            }
                break;
            case 1:
            {
                cell.textLabel.text = @"收件人";
                cell.textLabel.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
                [cell addSubview:self.personTF];
                [self.personTF mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo (-30);
                    make.top.mas_equalTo(4);
                    make.bottom.mas_equalTo(-4);
                    make.width.mas_equalTo(150);
                }];
                UserModel *model = userModel;
                self.personTF.text = [SecurityUtil decryptAESData:model.name];
                self.personTF.textAlignment = NSTextAlignmentRight;
                self.personTF.font = [UIFont systemFontOfSize:14];
                cell.detailTextLabel.textColor = [UIColor blackColor];
            }
                break;
            case 2:
            {
                cell.textLabel.text = @"联系电话";
                cell.textLabel.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
                [cell addSubview:self.phoneTF];
                [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo (-30);
                    make.top.mas_equalTo(4);
                    make.bottom.mas_equalTo(-4);
                    make.width.mas_equalTo(150);
                }];
                self.phoneTF.textAlignment = NSTextAlignmentRight;
                self.phoneTF.placeholder = @"联系电话";
                self.phoneTF.font = [UIFont systemFontOfSize:14];
            }
                break;
            case 3:
            {
                cell.textLabel.text = @"城市地址";
                cell.textLabel.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
                [cell addSubview:self.addressTF];
                [self.addressTF mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo (-30);
                    make.top.mas_equalTo(4);
                    make.bottom.mas_equalTo(-4);
                    make.width.mas_equalTo(150);
                }];
                self.addressTF.textAlignment = NSTextAlignmentRight;
                self.addressTF.placeholder = @"选择市区";
                self.addressTF.userInteractionEnabled = NO;
                self.addressTF.font = [UIFont systemFontOfSize:14];
                UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_arrows"]];
                iv.frame = CGRectMake(0, 0, 12, 12);
                iv.contentMode = UIViewContentModeScaleAspectFit;
                cell.accessoryView = iv;
            }
                break;
            default:
            {
                cell.textLabel.text = @"详细地址";
                cell.textLabel.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
                [cell addSubview:self.detailAddressTF];
                [self.detailAddressTF mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo (-30);
                    make.top.mas_equalTo(4);
                    make.bottom.mas_equalTo(-4);
                    make.width.mas_equalTo(150);
                }];
                self.detailAddressTF.textAlignment = NSTextAlignmentRight;
                self.detailAddressTF.placeholder = @"请输入详细地址";
                self.detailAddressTF.font = [UIFont systemFontOfSize:14];
            }
                break;

        }
        return  cell;
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            if ( !cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
                cell.textLabel.text = @"兑换信息";
            }
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
             return  cell;
        }else if(indexPath.row == 1){
            SubmitDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SubmitDetailTableViewCell" forIndexPath:indexPath];
            cell.iconIVW.constant = 90 / 320.0 * Width;
            cell.iconIVH.constant = 100 / 320.0  *Width;
            cell.priceLb.hidden = YES;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
            [cell.iconIV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.188:8080%@", self.dataDic[@"img"]]]];
            cell.titleLb.text = self.dataDic[@"name"];
            cell.titleLb.font = [UIFont systemFontOfSize:16];
            
            
            
            return  cell;
        }else {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            if ( !cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
                cell.textLabel.text = @"需要金币";
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@个", self.dataDic[@"price"]];
            }
            cell.textLabel.font = [UIFont systemFontOfSize:14];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return  cell;
        }
    
    }else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lastCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"lastCell"];
        }
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cell addSubview:btn];
        btn.backgroundColor = [UIColor colorWithRed:255/255.0 green:88/255.0 blue:88/255.0 alpha:1];
        [btn setTitle:@"兑换" forState:UIControlStateNormal];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.top.mas_equalTo(5);
            make.bottom.mas_equalTo(-5);
        }];
        [btn addTarget:self action:@selector(placeOrder) forControlEvents:UIControlEventTouchUpInside];
        btn.layer.cornerRadius = 8;
        btn.layer.masksToBounds = YES;
        cell.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
        return cell;
    }
}
//
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 44;
    }else if (indexPath.section == 1) {
        if (indexPath.row == 1) {
            return 115;
        }else {
            return 44;
        }
    }else {
        return 50;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        return 20 / 667.0 *Height;
    }else {
        return 10;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 3) {
        AreaViewController *vc = [[AreaViewController alloc] init];
        vc.hadChoseArea = ^(NSString *area){
            self.addressTF.text = area;
        };
        [self.navigationController pushViewController:vc animated:YES];
    }


}



/** 兑换*/
- (void)placeOrder {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请输入支付密码" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        ExchangeSucceedViewController *vc = [ExchangeSucceedViewController CreatExchangeSucceedViewController];
        [self creatNav:vc];
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        // 可以在这里对textfield进行定制，例如改变背景色
        textField.borderStyle = UITextBorderStyleNone;
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
